/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import db.LoadAll;
import model.DB;
import java.awt.Color;
import java.sql.Statement;
import java.util.ArrayList;
import model.Auction;
import model.Bid;

/**
 *
 * @author Morten
 */
public class LoginGUI extends javax.swing.JFrame {

    private String errormsg;
    private DB db;
    private LoadAll load;
    private boolean loginstatus;

    /**
     * Creates new form LoginGUI
     */
    public LoginGUI() {
	System.out.println("TESTlogin173985");
	initComponents();
	loginstatus = false;
	db = new DB();
    }

    public boolean checkLogin(){
	return loginstatus;
    }
    public Statement getStatement() {
	return db.getStatement();
    }

    public ArrayList<Auction> getProducts() {
	return load.getProducts();
    }
    public ArrayList<Bid> getBidList(Auction auction){
	return load.getBidList(auction);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLoginHostText = new javax.swing.JTextField();
        jLoginTitle = new javax.swing.JLabel();
        jLoginHostLabel = new javax.swing.JLabel();
        jLoginPortLabel = new javax.swing.JLabel();
        jLoginPortText = new javax.swing.JTextField();
        jLoginUsernameLabel = new javax.swing.JLabel();
        jLoginPasswordLabel = new javax.swing.JLabel();
        jLoginUsernameText = new javax.swing.JTextField();
        jLoginPasswordText = new javax.swing.JTextField();
        jLoginBtn = new javax.swing.JButton();
        jLoginErrorMsg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLoginHostText.setText("localhost");

        jLoginTitle.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLoginTitle.setText("Database - Login");

        jLoginHostLabel.setText("Host:");

        jLoginPortLabel.setText("Port:");

        jLoginPortText.setText("3306");

        jLoginUsernameLabel.setText("Brugernavn:");

        jLoginPasswordLabel.setText("Kode:");

        jLoginUsernameText.setText("root");

        jLoginPasswordText.setText("root");

        jLoginBtn.setText("OK");
        jLoginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLoginBtnActionPerformed(evt);
            }
        });

        jLoginErrorMsg.setFont(new java.awt.Font("Verdana", 0, 9)); // NOI18N
        jLoginErrorMsg.setForeground(new java.awt.Color(200, 50, 50));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLoginUsernameText))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLoginTitle)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLoginHostLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLoginHostText, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLoginPortLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLoginPortText, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLoginUsernameLabel)
                                    .addComponent(jLoginPasswordLabel))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLoginPasswordText))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLoginBtn))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLoginErrorMsg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLoginTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLoginHostLabel)
                    .addComponent(jLoginHostText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLoginPortLabel)
                    .addComponent(jLoginPortText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLoginUsernameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLoginUsernameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLoginPasswordLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLoginPasswordText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLoginErrorMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLoginBtn)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLoginBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLoginBtnActionPerformed

	int test = db.login(jLoginHostText.getText(), jLoginPortText.getText(), jLoginUsernameText.getText(), jLoginPasswordText.getText());
	Color red = new Color(200, 50, 50);
	jLoginHostText.setBackground(Color.white);
	jLoginPortText.setBackground(Color.white);
	jLoginUsernameText.setBackground(Color.white);
	jLoginPasswordText.setBackground(Color.white);
	switch (test) {
	    case 0:
		errormsg = "Databasen blev ikke fundet";
		jLoginHostText.setBackground(red);
		jLoginPortText.setBackground(red);
		break;
	    case 1045:
		errormsg = "Brugernavn og kodeord matcher ikke";
		jLoginUsernameText.setBackground(red);
		jLoginPasswordText.setBackground(red);
		break;
	    case 20000:
		errormsg = "Database driver ikke fundet";
		break;
	    case 10000:
		errormsg = "SUCCES!";
		loginstatus = true;
		load = LoadAll.getInstance();
		break;
	    default:
		errormsg = "Ukendt fejl!";
	}
	jLoginErrorMsg.setText(errormsg);
	if (test == 10000) {
	    try {
		Thread.sleep(2000);
	    } catch (InterruptedException ex) {
	    }
	    super.dispose();
	}
    }//GEN-LAST:event_jLoginBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
	/* Set the Nimbus look and feel */
	//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
	 */
	try {
	    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
		if ("Nimbus".equals(info.getName())) {
		    javax.swing.UIManager.setLookAndFeel(info.getClassName());
		    break;
		}
	    }
	} catch (ClassNotFoundException ex) {
	    java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (InstantiationException ex) {
	    java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (IllegalAccessException ex) {
	    java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	} catch (javax.swing.UnsupportedLookAndFeelException ex) {
	    java.util.logging.Logger.getLogger(LoginGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	//</editor-fold>

	/* Create and display the form */
	java.awt.EventQueue.invokeLater(new Runnable() {
	    public void run() {
		new LoginGUI().setVisible(true);
	    }
	});
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jLoginBtn;
    private javax.swing.JLabel jLoginErrorMsg;
    private javax.swing.JLabel jLoginHostLabel;
    private javax.swing.JTextField jLoginHostText;
    private javax.swing.JLabel jLoginPasswordLabel;
    private javax.swing.JTextField jLoginPasswordText;
    private javax.swing.JLabel jLoginPortLabel;
    private javax.swing.JTextField jLoginPortText;
    private javax.swing.JLabel jLoginTitle;
    private javax.swing.JLabel jLoginUsernameLabel;
    private javax.swing.JTextField jLoginUsernameText;
    // End of variables declaration//GEN-END:variables
}
